# -*- coding: utf-8 -*-
###############################################################################
# file    : dbinit.py<src>
# author  : wangyubin
# date    : Wed Apr 16 10:24:50 2014
# 
# brief   : 初始化数据库
# history : init
###############################################################################

from uliweb import settings
# CM-box lib
from db_app.model_define import *
from comm_app import utils
from comm_app.my_log import App_Log

def init_bug_importance():
    """ 初始化 bug 重要性master
    """
    T_BUG_IMPORTANCE.remove()
    
    App_Log.logger.debug(u'初始化 bug 重要性master')
    bug_importance = T_BUG_IMPORTANCE()
    bug_importance.name = u"UI显示问题"
    bug_importance.code = 1
    bug_importance.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 1 :')
    App_Log.logger.debug(u'name: ' + bug_importance.name)
    App_Log.logger.debug(u'code: ' + repr(bug_importance.code))
    App_Log.logger.debug(u'===========================================')

    bug_importance = T_BUG_IMPORTANCE()
    bug_importance.name = u"功能无法使用"
    bug_importance.code = 2
    bug_importance.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 2 :')
    App_Log.logger.debug(u'name: ' + bug_importance.name)
    App_Log.logger.debug(u'code: ' + repr(bug_importance.code))
    App_Log.logger.debug(u'===========================================')

    bug_importance = T_BUG_IMPORTANCE()
    bug_importance.name = u"系统崩溃"
    bug_importance.code = 4
    bug_importance.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 3 :')
    App_Log.logger.debug(u'name: ' + bug_importance.name)
    App_Log.logger.debug(u'code: ' + repr(bug_importance.code))
    App_Log.logger.debug(u'===========================================')


def init_bug_state():
    """ 初始化 bug 状态master
    """
    App_Log.logger.debug(u'初始化 bug 状态master')
    T_BUG_STATE.remove()

    bug_state = T_BUG_STATE()
    bug_state.name = u"已创建"
    bug_state.code = 1
    bug_state.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 1 :')
    App_Log.logger.debug(u'name: ' + bug_state.name)
    App_Log.logger.debug(u'code: ' + repr(bug_state.code))
    App_Log.logger.debug(u'===========================================')

    bug_state = T_BUG_STATE()
    bug_state.name = u"已分派"
    bug_state.code = 2
    bug_state.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 2 :')
    App_Log.logger.debug(u'name: ' + bug_state.name)
    App_Log.logger.debug(u'code: ' + repr(bug_state.code))
    App_Log.logger.debug(u'===========================================')
    
    bug_state = T_BUG_STATE()
    bug_state.name = u"已对应"
    bug_state.code = 4
    bug_state.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 3 :')
    App_Log.logger.debug(u'name: ' + bug_state.name)
    App_Log.logger.debug(u'code: ' + repr(bug_state.code))
    App_Log.logger.debug(u'===========================================')
    
    bug_state = T_BUG_STATE()
    bug_state.name = u"已确认"
    bug_state.code = 8
    bug_state.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'新建数据 4 :')
    App_Log.logger.debug(u'name: ' + bug_state.name)
    App_Log.logger.debug(u'code: ' + repr(bug_state.code))
    App_Log.logger.debug(u'===========================================')
    

def init_admin_user():
    """ 初始化 admin 用户
    """
    App_Log.logger.debug(u'初始化 admin 用户')
    admin = T_USER()
    if T_USER.filter(T_USER.c.name == 'admin').count() > 0:
        admin = T_USER.filter(T_USER.c.name == 'admin').one()

    admin.name = "admin"
    admin.real_name = "admin"
    admin.passwd = utils.md5_crypt("admin")
    admin.email = "wangyb@chujuexinxi.com"
    admin.role  = settings.PERMISSION.SUPER
    admin.save()
    App_Log.logger.debug(u'===========================================')
    App_Log.logger.debug(u'admin用户信息 :')
    App_Log.logger.debug(u'name  : ' + admin.name)
    App_Log.logger.debug(u'real_name  : ' + admin.real_name)
    App_Log.logger.debug(u'passwd: ' + admin.passwd)
    App_Log.logger.debug(u'email : ' + admin.email)
    App_Log.logger.debug(u'===========================================')


init_bug_state()
init_bug_importance()
init_admin_user()

        
